package com.ruoyi.stock.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.stock.domain.WarehouseOutInfoNumber;
import com.ruoyi.stock.domain.vo.StockInventoryVo;
import com.ruoyi.stock.domain.vo.StockListVo;
import com.ruoyi.stock.domain.vo.WarehouseOutInfoNumberVo;
import com.ruoyi.stock.domain.vo.WarehouseOutStockVo;
import com.ruoyi.stock.mapper.WarehouseOutStockMapper;
import com.ruoyi.stock.service.WarehouseOutStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: YwaiX
 * @Version: 1.0
 * @Date: Created in 2024-10-31 19:07:52
 * @Description:
 */

@Service
public class WarehouseOutStockServiceImpl implements WarehouseOutStockService {

    @Autowired
    private WarehouseOutStockMapper warehouseOutStockMapper;

    @Override
    public WarehouseOutStockVo list(Integer warehouseOutInfoId, Integer goodsType) {
        return warehouseOutStockMapper.list(warehouseOutInfoId, goodsType);
    }

    @Override
    public List<StockListVo> stockList(String goodsNo,String sourceNo) {
        return warehouseOutStockMapper.stockList(goodsNo , sourceNo);
    }

    @Transactional
    @Override
    public Integer outStock(WarehouseOutInfoNumberVo warehouseOutInfoNumberVo) {
        warehouseOutStockMapper.update(warehouseOutInfoNumberVo);
        warehouseOutStockMapper.insert(warehouseOutInfoNumberVo);
        warehouseOutInfoNumberVo.setUpdateTime(DateUtils.getNowDate());
        List<StockInventoryVo> list = new ArrayList<>();
        for (WarehouseOutInfoNumber data : warehouseOutInfoNumberVo.getNumberList()) {
            StockInventoryVo stockInventoryVo = new StockInventoryVo(data.getWarehouseId(), warehouseOutInfoNumberVo.getGoodsNo(), data.getNumber(),DateUtils.getNowDate());
            list.add(stockInventoryVo);
        }

        for (StockInventoryVo vo : list) {
            warehouseOutStockMapper.updateInventory(vo);
        }
        return list.size();
    }

    @Transactional
    @Override
    public Integer updateWarehouseStatus(Integer warehouseOutId, Integer goodsType,String sourceNo,Long userId, Date nowDate) {
        if (goodsType == 2){
            warehouseOutStockMapper.updateSaleOrderStatus(sourceNo);
            warehouseOutStockMapper.updateSaleOut(sourceNo);
        }
        return warehouseOutStockMapper.updateWarehouseStatus(warehouseOutId,userId,nowDate);
    }
}
